/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.modules;

import alcatelparser.tags.DefaultParserTag;
import alcatelparser.tags.TagProperties;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author geniuz
 */
public class EmptyTagPrinter {
    public EmptyTagPrinter(IReducedBuilder ireducedbuilder)
    {
        _outBuilder = ireducedbuilder;
        _filterToPrint = new ArrayList();
        _filterToPrint.add(I.I(8619));
        setHeaderTags();
    }

    public final void printEmptyTag(String s)
    {
        if("".equals(s))
            return;
        String s1 = retrieveITag(s);
        if(s != null && _headerTags.contains(s.toUpperCase()))
            return;
        if(TagProperties.getTagProperties().getReducedTagProperty(I.I(6509)).equals(s))
            s1 = I.I(2098);
        if(_filterToPrint.contains(s) && !Env.getEnv().isCSVParsing())
        {
            return;
        } else
        {
            printSubList(getNotPrintedTags(s1));
            _curTag = s1;
            return;
        }
    }

    private boolean isHeaderTag(String s)
    {
        return false;
    }

    private List getNotPrintedTags(String s)
    {
        LinkedList linkedlist = (LinkedList)Env.getEnv().get(I.I(1194));
        int i = getStartIndex(linkedlist);
        int j = getEndIndex(linkedlist, s);
        if(j > i)
            return linkedlist.subList(i, j);
        else
            return new LinkedList();
    }

    private void printSubList(List list)
    {
        Iterator iterator = list.iterator();
        do
        {
            if(!iterator.hasNext())
                break;
            String s = (String)iterator.next();
            if(DefaultParserTag.isTagChecked(s))
                if(s.startsWith(I.I(8605)) && isPrintSupplementaryService(s))
                    (new SupplementaryServiceEmptyTagPrinter()).printEmptyTag(1, _outBuilder);
                else
                if(!s.startsWith(I.I(8605)))
                    printTag(s);
        } while(true);
    }

    private void printTag(String s)
    {
        String s1 = (String)((Map)Env.getEnv().get(I.I(3830))).get(s);
        if(!Env.getEnv().isFieldsDecoding())
        {
            _outBuilder.writeEmptyTagContent(1, 0, TagProperties.getTagProperties().getReducedTagProperty(s1), "");
        } else
        {
            Map map = (Map)Env.getEnv().get(I.I(273));
            if(map.containsKey(s))
                printeDecodedTypeTag(s1, map.get(s));
            else
                _outBuilder.writeEmptyTagContent(1, 0, TagProperties.getTagProperties().getReducedTagProperty(s1), "");
        }
    }

    private void printeDecodedTypeTag(String s, Object obj)
    {
        int i = ((Integer)obj).intValue();
        if(i == 4)
        {
            _outBuilder.writeEmptyTagContent(1, 4, TagProperties.getTagProperties().getReducedTagProperty(s), "");
            _outBuilder.writeEmptyTagContent(1, 5, "", "");
        } else
        if(i == 7)
        {
            if(DefaultParserTag.isTagChecked(I.I(5782)))
                _outBuilder.writeEmptyTagContent(1, 6, TagProperties.getTagProperties().getReducedTagProperty(s), "");
            if(DefaultParserTag.isTagChecked(I.I(5839)))
                _outBuilder.writeEmptyTagContent(1, 7, "", "");
            if(DefaultParserTag.isTagChecked(I.I(5874)))
                _outBuilder.writeEmptyTagContent(1, 8, "", "");
        } else
        if(i == 6)
        {
            if(DefaultParserTag.isTagChecked(I.I(5782)))
                _outBuilder.writeEmptyTagContent(1, 6, TagProperties.getTagProperties().getReducedTagProperty(s), "");
            if(DefaultParserTag.isTagChecked(I.I(5874)))
                _outBuilder.writeEmptyTagContent(1, 8, "", "");
        }
    }

    private boolean isPrintSupplementaryService(String s)
    {
        return !s.equals(I.I(6881));
    }

    private int getStartIndex(LinkedList linkedlist)
    {
        if(_curTag == null || linkedlist.indexOf(_curTag) == -1)
            return 0;
        else
            return linkedlist.indexOf(_curTag) + 1;
    }

    private int getEndIndex(LinkedList linkedlist, String s)
    {
        if(s == null || "".equals(s) || linkedlist.indexOf(s) == -1)
            return linkedlist.size();
        else
            return linkedlist.indexOf(s);
    }

    public final void release()
    {
        printEmptyTag(null);
        _curTag = null;
    }

    private String retrieveITag(String s)
    {
        if(s == null)
            return null;
        if(s.startsWith(I.I(8549)))
            return I.I(6823);
        if(s.startsWith(I.I(8576)))
            return I.I(6881);
        else
            return (String)RedTag2TagID.getInstance().get(s);
    }

    private void setHeaderTags()
    {
        _headerTags = new ArrayList();
        _headerTags.add(getHeaderTagValue(I.I(5407)));
        _headerTags.add(getHeaderTagValue(I.I(7464)));
        _headerTags.add(getHeaderTagValue(I.I(7376)));
        _headerTags.add(getHeaderTagValue(I.I(7398)));
        _headerTags.add(getHeaderTagValue(I.I(7318)));
        _headerTags.add(getHeaderTagValue(I.I(7450)));
        _headerTags.add(getHeaderTagValue(I.I(7422)));
        _headerTags.add(getHeaderTagValue(I.I(7334)));
        _headerTags.add(getHeaderTagValue(I.I(7293)));
        _headerTags.add(getHeaderTagValue(I.I(7352)));
    }

    private String getHeaderTagValue(String s)
    {
        return DefaultParserTag.getTagPropertiesValue(s).toUpperCase();
    }

    public static final String SUPPLEMENTARY_PREFIX = "Supplementary";
    private ArrayList _filterToPrint;
    private String _curTag;
    private IReducedBuilder _outBuilder;
    private ArrayList _headerTags;

}
